//
//  UIKStepper.swift
//  Preferential
//
//  Created by Fineland on 2021/3/16.
//

import UIKit

class UIKStepper: UIView {

    //左右label
    private var leftLabel: UILabel!
    private var rightLabel: UILabel!
    //值
    private var valueTextField: UITextField!
    
    //最大小值
    var max: Int = 100
    var min: Int = 0
    //闭包
    typealias ReturnClosure = (_ value: Int) -> Void
    //回调
    var valueChanged: ReturnClosure?
    
    var value: Int {
        
        set {
            valueTextField.text = "\(min)"
            if newValue >= min && newValue <= max {
                valueTextField.text = "\(newValue)"
            }
        }
        get {
            //返回
            return Int(valueTextField.text ?? "0")!
        }
    }
    
    override init(frame: CGRect) {
        let tempSize = CGSize(width: 124.0, height: 32.0)
        super.init(frame: CGRect(origin: CGPoint.zero, size: tempSize))

        //创建视图
        setupViews(in: tempSize)
        //属性
        self.backgroundColor = UIColor.white
        self.clipsToBounds = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    private func setupViews(in size: CGSize) -> Void {
        
        //更多属性
        self.layer.borderWidth = 0.5
        self.layer.borderColor = NSKVar.SEPERATOR_COLOR.cgColor
        self.layer.cornerRadius = 4.0
        //+(右视图)
        let wVal: CGFloat = 36.0
        rightLabel = label(with: CGRect(x: size.width - wVal, y: 0.0, width: wVal, height: size.height), text: "+", tag: 1)
        //-(左视图)
        leftLabel = label(with: CGRect(x: 0.0, y: 0.0, width: wVal, height: size.height), text: "-", tag: -1)
        //值
        valueTextField = UITextField(frame: CGRect(x: wVal, y: 0.0, width: size.width - 2.0 * wVal, height: size.height))
        valueTextField.font = UIFont.systemFont(ofSize: 16.0)
        valueTextField.textColor = NSKVar.C3_COLOR
        valueTextField.borderStyle = .none
        valueTextField.isEnabled = false
        valueTextField.textAlignment = .center
        //添加
        self.addSubview(valueTextField)
    }

    @objc private func selectRecognizer(_ sender: UITapGestureRecognizer) -> Void {
        
       //是否为空？
        guard let tempLabel = sender.view as? UILabel else {
            return
        }
        //更新值
        let tempVal = self.value + tempLabel.tag
        self.value = tempVal
        //回调是否为空？
        if let returnClosure = self.valueChanged {
            returnClosure(tempVal)
        }
    }
    
    private func label(with frame: CGRect, text: String, tag: Int) -> UILabel {
        
        //创建
        let tempLabel = UILabel(frame: frame)
        tempLabel.textColor = NSKVar.C9_COLOR
        tempLabel.font = UIFont.systemFont(ofSize: 21.0)
        tempLabel.tag = tag
        tempLabel.text = text
        tempLabel.textAlignment = .center
        tempLabel.layer.borderWidth = self.layer.borderWidth
        tempLabel.layer.borderColor = self.layer.borderColor
        tempLabel.isUserInteractionEnabled = true
        //添加
        self.addSubview(tempLabel)
        //添加事件
        let tempRecognizer = UITapGestureRecognizer(target: self, action: #selector(selectRecognizer(_:)))
        tempLabel.addGestureRecognizer(tempRecognizer)
        //返回
        return tempLabel
    }
}
